#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <sys/socket.h>
#include <pthread.h>

// 定义数据缓冲区和锁
#define BUFFER_SIZE 256
#define CAN_FRAME_SIZE 8
unsigned char buffer[BUFFER_SIZE];
int buffer_index = 0;
pthread_mutex_t buffer_lock;

// 串口文件描述符
int serial_fd;
int can_socket;

// 初始化串口
int init_serial(const char *device) {
    int fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd == -1) {
        perror("open_port: Unable to open /dev/ttyUSB0");
        return -1;
    }

    struct termios options;
    tcgetattr(fd, &options);
    cfsetispeed(&options, B9600);
    cfsetospeed(&options, B9600);
    options.c_cflag |= (CLOCAL | CREAD);
    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    tcsetattr(fd, TCSANOW, &options);

    return fd;
}

// 初始化 CAN 套接字
int init_can_socket(const char *ifname) {
    int s;
    struct sockaddr_can addr;
    struct ifreq ifr;

    s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
    if (s < 0) {
        perror("Socket");
        return -1;
    }

    strcpy(ifr.ifr_name, ifname);
    ioctl(s, SIOCGIFINDEX, &ifr);

    addr.can_family = AF_CAN;
    addr.can_ifindex = ifr.ifr_ifindex;

    if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
        perror("Bind");
        return -1;
    }

    return s;
}

// 串口读取线程函数
void *serial_read_thread(void *arg) {
    int bytes_read;
    while (1) {
        bytes_read = read(serial_fd, buffer + buffer_index, BUFFER_SIZE - buffer_index);
        if (bytes_read > 0) {
            pthread_mutex_lock(&buffer_lock);
            buffer_index += bytes_read;
            pthread_mutex_unlock(&buffer_lock);
        }
    }
    return NULL;
}

// CAN 发送线程函数
void *can_send_thread(void *arg) {
    struct can_frame frame;
    while (1) {
        pthread_mutex_lock(&buffer_lock);
        while (buffer_index >= CAN_FRAME_SIZE) {
            frame.can_id = 0x000;
            frame.can_dlc = CAN_FRAME_SIZE;
            memcpy(frame.data, buffer, CAN_FRAME_SIZE);
            
            // 移动缓冲区中剩余的数据
            memmove(buffer, buffer + CAN_FRAME_SIZE, buffer_index - CAN_FRAME_SIZE);
            buffer_index -= CAN_FRAME_SIZE;

            if (write(can_socket, &frame, sizeof(struct can_frame)) != sizeof(struct can_frame)) {
                perror("Write");
            }
        }
        pthread_mutex_unlock(&buffer_lock);
        usleep(1000); // 避免忙等待
    }
    return NULL;
}

int main() {
    // 初始化串口
    serial_fd = init_serial("/dev/ttyUSB0");
    if (serial_fd == -1) {
        return 1;
    }

    // 初始化 CAN 套接字
    can_socket = init_can_socket("can0");
    if (can_socket == -1) {
        return 1;
    }

    // 初始化缓冲区锁
    pthread_mutex_init(&buffer_lock, NULL);

    // 创建线程
    pthread_t serial_thread, can_thread;
    pthread_create(&serial_thread, NULL, serial_read_thread, NULL);
    pthread_create(&can_thread, NULL, can_send_thread, NULL);

    // 等待线程结束
    pthread_join(serial_thread, NULL);
    pthread_join(can_thread, NULL);

    close(serial_fd);
    close(can_socket);
    return 0;
}
